class ViewUtils {
	public static getboundsOfView(view:egret.DisplayObject):ViewRect{
		if(!view){
			return null;
		}
		let bounds = new ViewRect();
		bounds.x = 0;
		bounds.y = 0;
		bounds.width = view.width;
		bounds.height = view.height;
		return bounds;
	}
	public static setboundsOfView(view:egret.DisplayObject,bounds:ViewRect):void{
		if(!view){
			return null;
		}
		view.width = bounds.width;
		view.height = bounds.height;
	}
	public static getframeOfView(view:egret.DisplayObject):ViewRect{
		if(!view){
			return null;
		}
		let frame = new ViewRect();
		frame.x = view.x;
		frame.y = view.y;
		frame.width = view.width;
		frame.height = view.height;
		return frame;
	}
	public static setframeOfView(view:egret.DisplayObject,frame:ViewRect):void{
		if(!view){
			return null;
		}
		view.x = frame.x;
		view.y = frame.y;
		view.width = frame.width;
		view.height = frame.height;
	}
	public static getSizeOfView(view:egret.DisplayObject):ViewRect{
		if(!view){
			return null;
		}
		let frame = new ViewRect();
		frame.x = 0;
		frame.y = 0;
		frame.width = view.width;
		frame.height = view.height;
		return frame;
	}
	public static setSizeOfView(view:egret.DisplayObject,bound:ViewRect):void{
		if(!view){
			return null;
		}
		view.width = bound.width;
		view.height = bound.height;
	}
	public static getOriginOfView(view:egret.DisplayObject):ViewPoint{
		if(!view){
			return null;
		}
		let point = new ViewPoint();
		point.x = view.x-view.anchorOffsetX;
		point.y = view.y-view.anchorOffsetY;
		return point;
	}
	public static setOriginOfView(view:egret.DisplayObject,point:ViewPoint):void{
		if(!view){
			return null;
		}
		view.x = point.x+view.anchorOffsetX;
		view.y = point.y+view.anchorOffsetY;
	}
	public static setTopLeftOfView(view:egret.DisplayObject,left:number,top:number):void{
		if(!view){
			return null;
		}
		view.x = left+view.anchorOffsetX;
		view.y = top+view.anchorOffsetY;
	}
	public static getOriginOfFrame(frame:ViewRect):ViewPoint{
		if(!frame){
			return null;
		}
		let point = new ViewPoint();
		point.x = frame.x;
		point.y = frame.y;
		return point;
	}
	public static setOriginOfFrame(frame:ViewRect,point:ViewPoint):void{
		if(!frame){
			return null;
		}
		frame.x = point.x;
		frame.y = point.y;
	}
	public static getCenterOfView(view:egret.DisplayObject):ViewPoint{
		if(!view){
			return null;
		}
		let center = new ViewPoint();
		center.x = view.x+view.width/2;
		center.y = view.y+view.height/2;
		return center;
	}
	public static setCenterOfView(view:egret.DisplayObject,center:ViewPoint):void{
		if(!view){
			return null;
		}
		view.x = center.x-view.width/2;
		view.y = center.y-view.height/2;
	}

	public static addChildWithParentBounds(parent:egret.DisplayObjectContainer,child:egret.DisplayObject){
		if(parent && child){
			let parentBounds = ViewUtils.getboundsOfView(parent);
			ViewUtils.setframeOfView(child,parentBounds);
			parent.addChild(child);
		}
	}
	public static addChildWithParentFrame(parent:egret.DisplayObjectContainer,child:egret.DisplayObject,frame:ViewRect){
		if(parent && child){
			ViewUtils.setframeOfView(child,frame);
			parent.addChild(child);
		}
	}
	public static addChildWithParentFrameAt(parent:egret.DisplayObjectContainer,child:egret.DisplayObject,frame:ViewRect,at:number){
		if(parent && child){
			ViewUtils.setframeOfView(child,frame);
			parent.addChildAt(child,at);
		}
	}

	/***********view2 */
	public static leftOfView(view:egret.DisplayObject):number{
		if(view){
			return view.x-view.anchorOffsetX;
		}else{
			return 0;
		}
	}
	public static rightOfView(view:egret.DisplayObject):number{
		if(view){
			return view.x-view.anchorOffsetX+view.width;
		}else{
			return 0;
		}
	}
	public static topOfView(view:egret.DisplayObject):number{
		if(view){
			return view.y-view.anchorOffsetY;
		}else{
			return 0;
		}
	}
	public static bottomOfView(view:egret.DisplayObject):number{
		if(view){
			return view.y-view.anchorOffsetY+view.height;
		}else{
			return 0;
		}
	}
	/********* rect*******/
	public static leftOfRect(rect:ViewRect):number{
		if(rect){
			return rect.x;
		}else{
			return 0;
		}
	}
	public static rightOfRect(rect:ViewRect):number{
		if(rect){
			return rect.x+rect.width;
		}else{
			return 0;
		}
	}
	public static topOfRect(rect:ViewRect):number{
		if(rect){
			return rect.y;
		}else{
			return 0;
		}
	}
	public static bottomOfRect(rect:ViewRect):number{
		if(rect){
			return rect.y+rect.height;
		}else{
			return 0;
		}
	}
	/******** */
	public static originEqueal(view1:egret.DisplayObject,view2:egret.DisplayObject):boolean{
		if(view1 && view2){
			if(view1.x == view2.x && view1.y == view2.y){
				return true;
			}
		}
		return false;
	}

	public static isRectEqual(rect1:ViewRect,rect2:ViewRect): boolean{
		if(rect1.x == rect2.x 
			&& rect1.y == rect2.y
			&& rect1.width == rect2.width
			&& rect1.height == rect2.height
			){
			return true;
		}
		return false;
	}
	public static addTapEvent(obj:egret.DisplayObject,func:Function, thisObj: any,options?:any){
		if(obj && func && thisObj){
			let listenKey = "_viewUtils_addTapEvent_listener"+obj.hashCode;
			let listener = thisObj[listenKey];
			// 将点击时设置的内容做处理
			if(options){
				let fromOptions = {};
				for(let key in options){
					let fromValue = obj[key];
					fromOptions[key] = fromValue;
				}
				obj["_viewUtils_from_option"] = fromOptions;
				obj["_viewUtils_to_option"] = options;
			}
			if(listener){
				obj.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,listener,thisObj);
				obj.removeEventListener(egret.TouchEvent.TOUCH_TAP,listener,thisObj);
			}else{
				listener = function(evt:egret.TouchEvent){
					let toOptions = obj["_viewUtils_to_option"];
					let fromOptions = obj["_viewUtils_from_option"];
					switch (evt.type) {
							case egret.TouchEvent.TOUCH_TAP:
								evt.stopPropagation();
							break;
							case egret.TouchEvent.TOUCH_BEGIN:
								obj.addEventListener(egret.TouchEvent.TOUCH_END,listener,thisObj);
								obj.addEventListener(egret.TouchEvent.TOUCH_CANCEL,listener,thisObj);
								obj.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,listener,thisObj);
								obj.addEventListener(egret.TouchEvent.TOUCH_MOVE,listener,thisObj);
								if(toOptions){
									for(let key in toOptions){
										let value = toOptions[key];
										obj[key] = value;
									}
								}
								func.call(thisObj, evt);
							break;
							case egret.TouchEvent.TOUCH_MOVE:
								func.call(thisObj, evt);
							break;
							case egret.TouchEvent.TOUCH_RELEASE_OUTSIDE:
								obj.removeEventListener(egret.TouchEvent.TOUCH_END,listener,thisObj);
								obj.removeEventListener(egret.TouchEvent.TOUCH_CANCEL,listener,thisObj);
								obj.removeEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,listener,thisObj);
								obj.removeEventListener(egret.TouchEvent.TOUCH_MOVE,listener,thisObj);
								if(fromOptions){
									for(let key in fromOptions){
										let value = fromOptions[key];
										obj[key] = value;
									}
								}
								func.call(thisObj, evt);
							break;
							case egret.TouchEvent.TOUCH_END:
								obj.removeEventListener(egret.TouchEvent.TOUCH_END,listener,thisObj);
								obj.removeEventListener(egret.TouchEvent.TOUCH_CANCEL,listener,thisObj);
								obj.removeEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,listener,thisObj);
								obj.removeEventListener(egret.TouchEvent.TOUCH_MOVE,listener,thisObj);
								if(fromOptions){
									for(let key in fromOptions){
										let value = fromOptions[key];
										obj[key] = value;
									}
								}
								func.call(thisObj, evt);
							break;
							case egret.TouchEvent.TOUCH_CANCEL:
								obj.removeEventListener(egret.TouchEvent.TOUCH_END,listener,thisObj);
								obj.removeEventListener(egret.TouchEvent.TOUCH_CANCEL,listener,thisObj);
								obj.removeEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,listener,thisObj);
								obj.removeEventListener(egret.TouchEvent.TOUCH_MOVE,listener,thisObj);
								if(fromOptions){
									for(let key in fromOptions){
										let value = fromOptions[key];
										obj[key] = value;
									}
								}
								func.call(thisObj, evt);
							break;
						}
					thisObj[listenKey] = listener;
				}
			}
			obj.addEventListener(egret.TouchEvent.TOUCH_BEGIN,listener,thisObj);
			obj.addEventListener(egret.TouchEvent.TOUCH_TAP,listener,thisObj);
		}
	}
	public static removeTapEvent(obj:egret.DisplayObject,thisObj: any){
		if(obj && thisObj){
			let listenKey = "_viewUtils_addTapEvent_listener"+obj.hashCode;
			let listener = thisObj[listenKey];
			if(listener){
				obj.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,listener,thisObj);
				obj.removeEventListener(egret.TouchEvent.TOUCH_TAP,listener,thisObj);
			}
			thisObj[listenKey] = null;
		}
	}
	
}

